#ifndef __CSP_H__ //class of Serial Port 
#define __CSP_H__
#include "CDBG.h"

#ifdef _WIN32_WCE
	#define GENsprintf   wsprintf
	#define GENstrlen	wcslen
	#define GENstrcmp	wcscmp
#else
	#define GENsprintf	sprintf
	#define GENstrlen	strlen
	#define GENstrcmp	strcmp
#endif

//#define CSP_DEBUG_ON
#ifdef	CSP_DEBUG_ON
	//#define CSP_ENABLE_READ_WRITE_LOG		// whether write "read/write message" in log
#endif

#define SP_DATA_TIMEOUT		2			// seconds. read/write com port time-out value 
#define RX_BUFFER			500*1024		// buffer in device
#define TX_BUFFER			16*1024		// buffer in device


//return values of this module
enum 
{
	CSP_OK,						//Success
	CSP_OPEN_PORT_FAIL,			//open com port fail, maybe the port is already by other ap
	CSP_INVALID_HANDLE,			//com port handle is invalid
	CSP_TIMEOUT,				//Read/Write data timeout
	CSP_CONNECT_FAIL,			//connection is broken
	CSP_FAIL,					//other errors
} ; 


class CSP
{
private:
	int		m_iBaudrate;
	CDBG	DBGobj;


public:
	HANDLE	m_hPort;

	// constructor
	CSP();
   // destructor
	~CSP() {Close();};
	int Open( TCHAR *szPortName, int iBaudrate);
	int Close();
	int SetBaudrate(int iBaudrate);
	int WriteLog(char* szMsg);
	//check if com port handle is valid
	bool IfConnected ();
	int Read(BYTE* pbReadData, DWORD nRequestLen,DWORD * piActualReadLen);
	int Write(BYTE* pbWriteData, DWORD nRequestLen,DWORD * piActualWriteLen);
	int PurgeTxBuffer();
	int PurgeRxBuffer();

};

#endif